#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;
use rocket::request::Form;
use serde::{Deserialize, Serialize};
use rocket_contrib::json::Json;

// use futures::future::{ready, Ready};

// use rbatis::crud::{CRUD, CRUDEnable};
// use rbatis::plugin::logic_delete::RbatisLogicDeletePlugin;
// use rbatis::plugin::page::{Page, PageRequest};
// use rbatis::rbatis::Rbatis;
// use rbatis::core::Error as rbatis_error;
// use rbatis_macro_driver::sql;
// use chrono::NaiveDateTime;
// use bigdecimal_::BigDecimal;
// use rbatis::core::value::DateTimeNow;
// use fast_log::filter::{ModuleFilter};
// use rbatis::core::db_adapter::DBExecResult;

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

#[get("/hello/<name>/<age>/<cool>")]
fn hello(name: String, age: u8, cool: bool) -> String {
    if cool {
        format!("You're a cool {} year old, {}!", age, name)
    } else {
        format!("{}, we need to talk about your coolness.", name)
    }
}

#[derive(FromForm)]
#[derive(Serialize, Deserialize, Clone, Debug)]
struct Task {
    complete: bool,
    description: String,
}

#[post("/todo", data = "<task>")]
fn new(task: Form<Task>) {
    println!("{:?}, {}", task.complete, task.description)
}

#[derive(Deserialize)]
struct JsonTask {
    description: String,
    complete: bool
}

#[post("/json_todo", data = "<json_task>")]
fn json_todo(json_task: Json<JsonTask>) -> Json<Task>{
    println!("{:?}, {}", json_task.complete, json_task.description);

    Json(Task{
        complete: false,
        description: String::from("Rocket!!!!!"),
    })
}

#[get("/query?<name>&<age>&<sex>")]
fn query(name: String, age :u32, sex :String) -> String {
    format!("{}, {}, {}", name, age, sex)
}

// #[sql(rbatis, "select * from store")]
// async fn select_store(rbatis: &Rbatis , tx_id:&str) -> Vec<Store> {}

// #[sql(rbatis, "select * from biz_activity where store_id = ?")]
// async fn select_biz_activity_by_store_id(rbatis: &Rbatis , tx_id:&str , store_id: String) -> Vec<BizActivity> {}

// #[sql(rbatis, "select c.*  from commodity c inner join activity_commodity ac on c.id = ac. commodity_id where ac.activity_id = ?")]
// async fn query_activity_commodity(rbatis: &Rbatis , tx_id:&str , activity_id: String) -> Vec<Commodity> {}

// #[post("/queryAtivity")]
// fn query_activity() -> Json<Vec<Store>>{

//     let rbatis =  init_rbatis().await;
//     let tx_id = "3";//事务id号
//     rbatis.begin(tx_id).await.unwrap();

//     let store_list = select_store(&rbatis, &tx_id).await;
//     if  let Ok(mut s_list) = store_list{
//         for store in &mut s_list{
//             match &store.id {
//                 Some(s_id) => {
//                     let ba_list = select_biz_activity_by_store_id(&rbatis, &tx_id, s_id.to_string()).await;
            
//                     if let Ok(mut list) = ba_list{
//                         store.biz_activity_list = Some(list.clone());
//                         println!("store.biz_activity_list = {:?}", store.biz_activity_list);
//                         if let Some(sba_list) = &mut store.biz_activity_list{
//                             for activity in sba_list{
//                                 match &activity.id{
//                                     Some(a_id) => {
//                                         let c_list = query_activity_commodity(&rbatis, &tx_id, a_id.to_string()).await;
//                                         if let Ok(l) = c_list{
//                                             println!("l = {:?}", l);
//                                             activity.commodity_list = Some(l);
//                                         }
//                                         println!("activity.commodity_list = {:?}", activity.commodity_list);
//                                     },
//                                     None =>{

//                                     }
//                                 }
//                             }
//                         }
//                     }
//                 },
//                 None =>{

//                 }
//             }
//         }

//         rbatis.commit(tx_id).await.unwrap();

//       Json(s_list)

//     }else{
//         let list: Vec<Store> = Vec::new();
//         Json(list)
//     }
// }

// #[macro_use]
// extern crate rbatis_macro_driver;

// #[crud_enable( id_name:id |  id_type:String |  table_name:biz_activity |  table_columns:id,name,store_id,pc_link,h5_link,pc_banner_img,h5_banner_img,sort,status,remark,create_time,update_time,version,delete_flag )]
// #[derive(Serialize, Deserialize, Clone, Debug)]
// // #[derive(CRUDEnable,Serialize, Deserialize, Clone, Debug)] 
// pub struct BizActivity {
//     pub id: Option<String>,
//     pub name: Option<String>,
//     pub store_id: Option<String>,
//     pub pc_link: Option<String>,
//     pub h5_link: Option<String>,
//     pub pc_banner_img: Option<String>,
//     pub h5_banner_img: Option<String>,
//     pub sort: Option<String>,
//     pub status: Option<i32>,
//     pub remark: Option<String>,
//     pub create_time: Option<NaiveDateTime>,
//     pub update_time: Option<NaiveDateTime>,
//     pub version: Option<BigDecimal>,
//     pub delete_flag: Option<i32>,
//     pub commodity_list: Option<Vec<Commodity>>,
// }

// #[crud_enable( id_name:id |  id_type:String |  table_name:commodity |  table_columns:id,name,remark,create_time,update_time,version,delete_flag )]
// #[derive(Serialize, Deserialize, Clone, Debug)]
// pub struct Commodity{
//     pub id: Option<String>, 
//     pub name: Option<String>,
//     pub remark: Option<String>,
//     pub create_time: Option<NaiveDateTime>,
//     pub update_time: Option<NaiveDateTime>,
//     pub version: Option<BigDecimal>,
//     pub delete_flag: Option<i32>,
// }

// #[crud_enable( id_name:id |  id_type:String |  table_name:activity_commodity |  table_columns:id,activity_id,activity_name,commodity_id,commodity_name,remark,create_time,update_time,version,delete_flag )]
// #[derive(Serialize, Deserialize, Clone, Debug)]
// pub struct ActivityCommodity{
//     pub id: Option<String>, 
//     pub activity_id: Option<String>, 
//     pub activity_name: Option<String>,
//     pub commodity_id: Option<String>, 
//     pub commodity_name: Option<String>,
//     pub remark: Option<String>,
//     pub create_time: Option<NaiveDateTime>,
//     pub update_time: Option<NaiveDateTime>,
//     pub version: Option<BigDecimal>,
//     pub delete_flag: Option<i32>,
// }

// #[crud_enable( id_name:id |  id_type:String |  table_name:store |  table_columns:id,name,remark,adress,create_time,update_time,version,delete_flag )]
// #[derive(Serialize, Deserialize, Clone, Debug)]
// pub struct  Store{
//     pub id: Option<String>, 
//     pub name: Option<String>,
//     pub remark: Option<String>,
//     pub adress: Option<String>,
//     pub create_time: Option<NaiveDateTime>,
//     pub update_time: Option<NaiveDateTime>,
//     pub version: Option<BigDecimal>,
//     pub delete_flag: Option<i32>,
//     pub biz_activity_list: Option<Vec<BizActivity>>,
// }

// fn init_rbatis() ->Rbatis{
//     let rb = Rbatis::new();
//     ///连接数据库,自动判断驱动类型"mysql://*","postgres://*","sqlite://*","mssql://*"加载驱动   
//     rb.link("mysql://root:123456@localhost:3306/rbatis_test").await.unwrap();
//     ///自定义连接池参数。(可选)
//     // let mut opt =PoolOptions::new();
//     // opt.max_size=100;
//     // rb.link_opt("mysql://root:123456@localhost:3306/test",&opt).await.unwrap();

//     rb
// }

// #[sql(rbatis, "select c.*  from commodity c inner join activity_commodity ac on c.id = ac. commodity_id where ac.activity_id = ?")]
// fn query_activity_commodity(rbatis: &Rbatis , tx_id:&str , activity_id: &str) -> Vec<Commodity> {}

fn main() {
    rocket::ignite().mount("/", routes![index, hello, new, json_todo, query]).launch();
}
